//
//  GetCodeOfCityManager.m
//  qwer
//
//  Created by laouhn on 15/12/9.
//  Copyright (c) 2015年 Heixiao. All rights reserved.
//

#import "GetCodeOfCityManager.h"

@interface GetCodeOfCityManager ()
@property (nonatomic, retain) NSMutableArray *dataArray;
@end
static GetCodeOfCityManager *manager;
@implementation GetCodeOfCityManager
+ (GetCodeOfCityManager *)sharedWithManager {
    if (manager == nil) {
        manager = [[GetCodeOfCityManager alloc] init];
    }
    return manager;
}
+ (NSString *)getCodeOfCity:(NSString *)city {
    manager = [GetCodeOfCityManager sharedWithManager];
    [manager getDataByJson];
    for (NSDictionary *dic in manager.dataArray) {
        if ([city isEqualToString:[dic objectForKey:@"市名"]]) {
            return [dic objectForKey:@"编码"];
        }
    }
    return nil;
}
- (void)getDataByJson {
    if (manager.dataArray == nil) {
        manager.dataArray = [NSMutableArray array];
    }
    NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"CodeOfCity" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:jsonPath];
    NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSMutableArray *array = [dic objectForKey:@"城市代码"];
    for (NSDictionary *dic  in array) {
        NSMutableArray *twoArray = [dic objectForKey:@"市"];
        for (NSDictionary *dic in twoArray) {
            [manager.dataArray addObject:dic];
        }
    }
    
}
@end
